package com.tiger.service.transactional;

import com.alibaba.fastjson.JSON;
import com.tiger.utils.RedisUtils;
import com.tiger.mapper.base.InterfaceMapper;
import com.tiger.pojo.base.Interface;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @author HuXuehao (StudiousTiger)
 * @desc 这个类的作用是
 * @date 2022/6/25
 */
@Component
public class InterfaceTransactional {
    @Autowired
    private InterfaceMapper interfaceMapper;
    @Autowired
    private WebApplicationContext applicationContext;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 重置接口信息
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void resetInterface() throws Exception{
        interfaceMapper.delete(null);
        List<Interface> interfaceInfo = getInterfaceInfo();
        for (Interface anInterface : interfaceInfo) {
            interfaceMapper.insert(anInterface);
            // 同步缓存
            String[] arr = new String[2];
            arr[0] = anInterface.getRole();
            arr[1] = anInterface.getDisableTag()+"";
            redisUtils.hset("interfaceInfo",anInterface.getUrl(), JSON.toJSONString(arr));
        }
    }
    /**
     * 同步接口信息
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void synchronizationInterface() throws Exception{
        List<Interface> interfaceInfoNew = getInterfaceInfo();
        List<Interface> interfaceInfoOld = interfaceMapper.selectList(null);
        HashMap<String, Boolean> map = new HashMap<>();
        for (Interface anInterface : interfaceInfoOld) {
            map.put(anInterface.getUrl(),true);
        }
        for (Interface anInterface : interfaceInfoNew) {
            if (!map.get(anInterface.getUrl())) {
                interfaceMapper.insert(anInterface);
                // 同步缓存
                String[] arr = new String[2];
                arr[0] = anInterface.getRole();
                arr[1] = anInterface.getDisableTag()+"";
                redisUtils.hset("interfaceInfo",anInterface.getUrl(), JSON.toJSONString(arr));
            }
        }
    }



    /**
     * 获取所有的接口
     * @return
     */
    public List<Interface> getInterfaceInfo() {
        Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(RestController.class);

        List<Interface> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : controllers.entrySet()) {
            Object value = entry.getValue();
            Class<?> aClass = AopUtils.getTargetClass(value);
            List<Method> methods = Arrays.asList(aClass.getMethods());
            for (Method method : methods) {

                RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                GetMapping getMapping = method.getAnnotation(GetMapping.class);
                PostMapping postMapping = method.getAnnotation(PostMapping.class);
                DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
                PutMapping putMapping = method.getAnnotation(PutMapping.class);
                list = mappingToList(
                        list,
                        requestMapping,
                        getMapping,
                        postMapping,
                        deleteMapping,
                        putMapping);
            }
        }
        return list;
    }

    /**
     * 将 arr 键添加到 list 中
     * @param list  用于存放接口的list
     * @param requestMapping
     * @param getMapping
     * @param postMapping
     * @param deleteMapping
     * @param putMapping
     * @return 添加完成数据后的list
     */
    private List<Interface> mappingToList(List<Interface> list,
                                          RequestMapping requestMapping,
                                          GetMapping getMapping,
                                          PostMapping postMapping,
                                          DeleteMapping deleteMapping,
                                          PutMapping putMapping){
        Interface anInterface = new Interface();
        if (requestMapping != null && requestMapping.value().length > 0) {
            anInterface.setUrl(requestMapping.value()[0].split("\\{")[0]);
            anInterface.setDeleteTag(0);
            anInterface.setDisableTag(0);
            String[] split = requestMapping.name().split("/");
            if (split.length == 2) {
                anInterface.setName(split[0]);
                anInterface.setRole(split[1]);
            } else if (split.length == 1) {
                anInterface.setName(split[0]);
                anInterface.setRole("暂无");
            } else if (split.length == 0) {
                anInterface.setName("暂无");
                anInterface.setRole("暂无");
            }
            list.add(anInterface);
        }
        if (getMapping != null && getMapping.value().length > 0) {
            anInterface.setUrl(getMapping.value()[0].split("\\{")[0]);
            anInterface.setDeleteTag(0);
            anInterface.setDisableTag(0);
            String[] split = getMapping.name().split("/");
            if (split.length == 2) {
                anInterface.setName(split[0]);
                anInterface.setRole(split[1]);
            } else if (split.length == 1) {
                anInterface.setName(split[0]);
                anInterface.setRole("暂无");
            } else if (split.length == 0) {
                anInterface.setName("暂无");
                anInterface.setRole("暂无");
            }
            list.add(anInterface);
        }
        if (postMapping != null && postMapping.value().length > 0) {
            anInterface.setUrl(postMapping.value()[0].split("\\{")[0]);
            anInterface.setDeleteTag(0);
            anInterface.setDisableTag(0);
            String[] split = postMapping.name().split("/");
            if (split.length == 2) {
                anInterface.setName(split[0]);
                anInterface.setRole(split[1]);
            } else if (split.length == 1) {
                anInterface.setName(split[0]);
                anInterface.setRole("暂无");
            } else if (split.length == 0) {
                anInterface.setName("暂无");
                anInterface.setRole("暂无");
            }
            list.add(anInterface);
        }
        if (deleteMapping != null && deleteMapping.value().length > 0) {
            anInterface.setUrl(deleteMapping.value()[0].split("\\{")[0]);
            anInterface.setDeleteTag(0);
            anInterface.setDisableTag(0);
            String[] split = deleteMapping.name().split("/");
            if (split.length == 2) {
                anInterface.setName(split[0]);
                anInterface.setRole(split[1]);
            } else if (split.length == 1) {
                anInterface.setName(split[0]);
                anInterface.setRole("暂无");
            } else if (split.length == 0) {
                anInterface.setName("暂无");
                anInterface.setRole("暂无");
            }
            list.add(anInterface);
        }
        if (putMapping != null && putMapping.value().length > 0) {
            anInterface.setUrl(putMapping.value()[0].split("\\{")[0]);
            anInterface.setDeleteTag(0);
            anInterface.setDisableTag(0);
            String[] split = putMapping.name().split("/");
            if (split.length == 2) {
                anInterface.setName(split[0]);
                anInterface.setRole(split[1]);
            } else if (split.length == 1) {
                anInterface.setName(split[0]);
                anInterface.setRole("暂无");
            } else if (split.length == 0) {
                anInterface.setName("暂无");
                anInterface.setRole("暂无");
            }
            list.add(anInterface);
        }
        return list;
    }
}
